package jeecg.business.controller.site;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jeecg.business.entity.site.SimpleSiteEntity;
import jeecg.business.service.site.SimpleSiteServiceI;
import jeecg.system.service.SystemService;

import org.apache.log4j.Logger;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.clean.HtmlCleanBaseUtil;
import org.jeecgframework.core.util.http.HttpClientUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**   
 * @Title: Controller
 * @Description: 简单站点
 * @author LinYingPiaoJian
 * @date 2015-08-04 16:33:01
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/simpleSiteController")
public class SimpleSiteController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SimpleSiteController.class);

	@Autowired
	private SimpleSiteServiceI simpleSiteService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 简单站点列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "simpleSite")
	public ModelAndView simpleSite(HttpServletRequest request) {
		request.setAttribute("nmstypestr", systemService.getReplaceStr4Group("nmstype"));
		return new ModelAndView("jeecg/business/site/simpleSiteList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(SimpleSiteEntity simpleSite,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(SimpleSiteEntity.class, dataGrid);
		//查询条件组装器
		if(simpleSite != null){
			if(StringUtil.isNotEmpty(simpleSite.getZdbs()))
				simpleSite.setZdbs("*"+simpleSite.getZdbs()+"*");
		}
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, simpleSite);
		this.simpleSiteService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除简单站点
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(SimpleSiteEntity simpleSite, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		simpleSite = systemService.getEntity(SimpleSiteEntity.class, simpleSite.getId());
		message = "删除成功";
		simpleSiteService.delete(simpleSite);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}
	
	@RequestMapping(params = "testXpath")
	@ResponseBody
	public AjaxJson testXpath(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try{
			String bmlx = request.getParameter("bmlx");
			String yldz = request.getParameter("testurl");
			String zwnr = request.getParameter("zwnr");
			
			String html = HttpClientUtil.getSourceFromUrl(yldz, bmlx, null);
			HtmlCleaner cleaner = new HtmlCleaner();
			TagNode node = cleaner.clean(html);
			String content = HtmlCleanBaseUtil.getTagContent(node, zwnr);
			content = StringUtil.removeHTMLLableExe(content);
			message = content;
			j.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			message = "抓取配置错误";
			j.setSuccess(false);
		}
		j.setMsg(message);
		return j;
	}
	
	


	/**
	 * 添加简单站点
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(SimpleSiteEntity simpleSite, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(simpleSite.getId())) {
			message = "更新成功";
			SimpleSiteEntity t = simpleSiteService.get(SimpleSiteEntity.class, simpleSite.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(simpleSite, t);
				simpleSiteService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			message = "添加成功";
			simpleSiteService.save(simpleSite);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		
		return j;
	}

	/**
	 * 简单站点列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(SimpleSiteEntity simpleSite, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(simpleSite.getId())) {
			simpleSite = simpleSiteService.getEntity(SimpleSiteEntity.class, simpleSite.getId());
			req.setAttribute("simpleSitePage", simpleSite);
		}
		return new ModelAndView("jeecg/business/site/simpleSite");
	}
}
